using System;
using System.Collections.Generic;
using System.Text;

using libsecondlife;
using libsecondlife.AssetSystem;
using libsecondlife.InventorySystem;

using Feathertail;

namespace NestBox.Inventory
{
    class asset : Feathertail.Command
    {
        FeathertailClient _Client;

        #region Command Members

        public void Setup(FeathertailClient Client)
        {
            _Client = Client;
        }

        public string HandleInput(string[] CmdLine)
        {
            if (CmdLine.Length < 2)
            {
                return GetHelpText();
            }

            if (CmdLine.Length == 3)
            {
                // Arbitrary Asset
                try
                {
                    LLUUID AssetID = new LLUUID(CmdLine[2]);
                    sbyte Type = sbyte.Parse(CmdLine[1]);

                    AssetRequestDownload request = _Client.Assets.RequestInventoryAsset(AssetID, Type);
                    if (request.Wait(AssetManager.DefaultTimeout) != AssetRequestDownload.RequestStatus.Success)
                    {
                        throw new Exception("Asset (" + AssetID.ToStringHyphenated() + ") unavailable (" + request.StatusMsg + ")");
                    }

                    return libsecondlife.Helpers.FieldToUTF8String(request.GetAssetData());
                }
                catch (Exception)
                {
                    return "Unable to find an asset with a UUID of: " + CmdLine[2];
                }
            }
            else
            {
                StringBuilder output = new StringBuilder();

                // Asset for an item in inventory
                string curDirectory = (string)_Client.FeathertailState[Helpers.CurrentDirectoryKey];
                InventoryFolder iFolder = _Client.Inventory.getFolder(curDirectory);

                iFolder.RequestDownloadContents(false, false, true).RequestComplete.WaitOne(15000, false);
                foreach (InventoryBase ib in iFolder.GetContents())
                {
                    if (ib is InventoryItem)
                    {
                        InventoryItem item = (InventoryItem)ib;
                        if (item.ItemID.Equals(CmdLine[1]))
                        {
                            output.AppendLine("Asset for " + item.Name + " [" + item.ItemID + "]");
                            output.AppendLine(item.Asset.AssetDataToString());
                        }

                        if (item.Name.Equals(CmdLine[1]))
                        {
                            output.AppendLine("Asset for " + item.Name + " [" + item.ItemID + "]");
                            output.AppendLine(item.Asset.AssetDataToString());
                        }
                        
                    }
                }

                return output.ToString();
            }
        }

        public string[] GetCommandStrings()
        {
            return new string[] { "asset", "getasset" };
        }

        public string GetSummaryText()
        {
            return "Retrieve and display an asset";
        }

        public string GetHelpText()
        {
            string help = "Usages:" + Environment.NewLine;
            help += "For arbitrary asset: getasset [type] [UUID]" + Environment.NewLine;
            help += "For asset of item (this works for notecards) : getasset [itemname | UUID]" + Environment.NewLine;
            help += "Examples:" + Environment.NewLine;
            help += "asset 13 c2ca25c1fb242e41650a54901bc2d21c" + Environment.NewLine;
            help += "asset \"New Script\"" + Environment.NewLine;

            return help;
        }

        #endregion
    }
}
